In [1]:
from numpy import *
from PIL import *
import pickle
from pylab import *
import os
In [2]:
import sift
import dsift
dsift = reload(dsift)
import imtools
imtools = reload(imtools)
In [3]:
def read_gesture_features_labels(path):
# make a list of the files with .dsift at the end
featlist = [os.path.join(path, f) for f in os.listdir(path)
if f.endswith('.dsift')]
# read features
features = []
for featfile in featlist:
l, d = sift.read_features_from_file(featfile)
features.append(d.flatten())
features = array(features)
# generate labels
labels = [featfile.split('/')[-1][0] for featfile in featlist]
return features, array(labels)
In [4]:
features, labels = read_gesture_features_labels('train2/')
test_features, test_labels = read_gesture_features_labels('test2/')
classnames = unique(labels)
In [5]:
# the first letter of the file name is the label
print labels
In [6]:
import knn
In [7]:
# k neighbors
k = 1
knn_classifier = knn.KnnClassifier(labels, features)
In [8]:
res = array([knn_classifier.classify(test_features[i], k) for i in
range(len(test_labels))])
# accuracy
acc = sum(1.0*(res==test_labels))/len(test_labels)
print 'Accuracy:', acc
Accuracy is lower than the number in the text book (0.811518324607)
In [9]:
def print_confusion(res, test_labels, classnames):
n = len(classnames)
class_ind = dict([(classnames[i], i) for i in range(n)])
confuse = zeros((n, n))
for i in range(len(test_labels)):
confuse[class_ind[res[i]], class_ind[test_labels[i]]] += 1
print 'Confusion matrix for'
print classnames
print confuse
In [10]:
print_confusion(res, test_labels, classnames)
In [11]:
import pca
In [74]:
V, S, m = pca.pca(array(features))
V = V[:75]
In [75]:
features2 = [dot(V, f-m) for f in features]
test_features2 = [dot(V, f-m) for f in test_features]
In [76]:
knn_classifier2 = knn.KnnClassifier(labels, features2)
In [77]:
res2 = array([knn_classifier2.classify(test_features2[i], k) for i in
range(len(test_labels))])
# accuracy
acc2 = sum(1.0*(res2==test_labels))/len(test_labels)
print 'Accuracy:', acc2
In [78]:
print_confusion(res2, test_labels, classnames)
Around 50-100 seems to be the sweet spot. Just before the curve in figure 8-9
In [ ]: